package com.example.fw;

import javax.swing.JDialog;

import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;


public abstract class HelperBase {
	protected final ApplicationManager manager;
	protected final JFrameOperator mainFrame;

	public HelperBase(ApplicationManager manager){
		this.manager = manager;		
		mainFrame = manager.getApplication();
	}
	
	protected String waitMessageDialog(String title, int timeout) {
		long start = System.currentTimeMillis();
		long currentTime = start;
		while(currentTime < start + timeout){
			JDialog dialog = JDialogOperator.findJDialog(mainFrame.getOwner(), title, false, false);
			if (dialog != null){
				JDialogOperator dialogOperator = new JDialogOperator(dialog);
				String message = new JLabelOperator(dialogOperator).getText();
				dialogOperator.requestClose();
				return message;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			currentTime = System.currentTimeMillis();
		}
		return null;
	}
}
